package fr.labri.macchiato.ui.preferences;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import fr.labri.macchiato.core.model.UserPreferences;

/**
 * 
 * Widget that needs to be fulfilled in order to offer products or models, close
 * to where the place the user lives. All the info are stored into the cache and
 * can be restored if the user checks the option 'Allows Macchiato to save all
 * my permanently'
 * 
 * 
 * @author Michelle AVOMO
 * @see AllPreferences
 * @see SaveInfosWidget
 * 
 * 
 * 
 */
public final class AddressWidget extends VLayout implements PreferencesWidget {

	private DynamicForm addressHouseForm = new DynamicForm();
	private DynamicForm addressProForm = new DynamicForm();

	private DataSource dataSourceHomeForm = new DataSource();
	private DataSource dataSourceProForm = new DataSource();

	private DataSourceTextField dsStreetHome;
	private DataSourceTextField dsCityHome;
	private DataSourceTextField dsPostcodeHome;
	private DataSourceTextField dsCountryHome;

	private DataSourceTextField dsStreetPro;
	private DataSourceTextField dsCityPro;
	private DataSourceTextField dsPostcodePro;
	private DataSourceTextField dsCountryPro;

	private static final String WIDGET_TITLE = "Mes adresses";

	/***************************************************** CONSTRUCTOR ************************************************************************/

	public AddressWidget() {

		// Text of the page
		HeaderItem header = new HeaderItem();
		header.setDefaultValue("Page de configuration de vos adresses domicile/travail");

		/************************************ Components for the main address form in terms of datasource *******************************************/

		dsStreetHome = new DataSourceTextField("streetHome", "Rue/Avenue", 50,
				true);
		dsCityHome = new DataSourceTextField("cityHome", "Ville", 50, true);
		dsPostcodeHome = new DataSourceTextField("postcodeHome", "Code postal",
				50, true);
		dsCountryHome = new DataSourceTextField("countryHome", "Pays", 50, true);

		/************************************ Components for the professional address *******************************************/

		dsStreetPro = new DataSourceTextField("streetPro", "Rue/Avenue", 50,
				true);
		dsCityPro = new DataSourceTextField("cityPro", "Ville", 50, true);
		dsPostcodePro = new DataSourceTextField("postcodePro", "Code postal",
				50, true);
		dsCountryPro = new DataSourceTextField("countryPro", "Pays", 50, true);

		/************************************ Datasource of the forms *******************************************/

		dataSourceHomeForm.setFields(dsStreetHome, dsCityHome, dsPostcodeHome,
				dsCountryHome);

		addressHouseForm.setDataSource(dataSourceHomeForm);
		addressHouseForm.setUseAllDataSourceFields(true);

		dataSourceProForm.setFields(dsStreetPro, dsCityPro, dsPostcodePro,
				dsCountryPro);
		addressProForm.setDataSource(dataSourceProForm);
		addressProForm.setUseAllDataSourceFields(true);

		/************************************ Properties of the forms *******************************************/

		// To fill the main address (y/n)

		addressHouseForm.setWidth100();
		addressHouseForm.setHeight100();
		addressHouseForm.setIsGroup(true);
		addressHouseForm.setGroupTitle("Domicile");
		addressHouseForm.setPadding(10);
		addressHouseForm.setRequiredMessage("Problème: Champs vide!");

		// To fill the second address

		addressProForm.setWidth100();
		addressProForm.setHeight100();
		addressProForm.setIsGroup(true);
		addressProForm.setGroupTitle("Travail/Universite/lycee");
		addressProForm.setPadding(5);

		/************************************ Default value of the forms *******************************************/

		addressHouseForm.setValue("countryHome", "France");
		addressProForm.setValue("countryPro", "France");

		addMember(addressHouseForm);
		addMember(addressProForm);

		HLayout toolbar = new HLayout();
		toolbar.setAlign(Alignment.CENTER);

	}

	/**
	 * updates the values of the both forms and add them as user preference
	 */
	public void collectUserPreferences(UserPreferences preference) {
		// collect address home infos
		preference.getHomeAddress().setStreet(
				addressHouseForm.getValueAsString("streetHome"));
		preference.getHomeAddress().setCity(
				addressHouseForm.getValueAsString("cityHome"));
		preference.getHomeAddress().setZipCode(
				addressHouseForm.getValueAsString("postcodeHome"));
		preference.getHomeAddress().setCountry(
				addressHouseForm.getValueAsString("countryHome"));

		// collect address pro infos

		preference.getWorkAddress().setStreet(
				addressProForm.getValueAsString("streetPro"));

		preference.getWorkAddress().setCity(
				addressProForm.getValueAsString("cityPro"));
		preference.getWorkAddress().setZipCode(
				addressProForm.getValueAsString("postcodePro"));
		preference.getWorkAddress().setCountry(
				addressProForm.getValueAsString("countryPro"));

	}

	/**
	 * Retrieves the infos about the addresses of the user previously filled in
	 * Macchiato
	 */
	public void restoreUserPreferences(UserPreferences preferences) {
		if (preferences.getHomeAddress() != null) {

			addressHouseForm.setValue("streetHome", preferences
					.getHomeAddress().getStreet());
			addressHouseForm.setValue("cityHome", preferences.getHomeAddress()
					.getCity());
			addressHouseForm.setValue("postcodeHome", preferences
					.getHomeAddress().getZipCode());
			addressHouseForm.setValue("countryHome", preferences
					.getHomeAddress().getCountry());

		}
		if (preferences.getWorkAddress() != null) {

			addressProForm.setValue("streetPro", preferences.getWorkAddress()
					.getStreet());

			addressProForm.setValue("cityPro", preferences.getWorkAddress()
					.getCity());
			addressProForm.setValue("postcodePro", preferences.getWorkAddress()
					.getZipCode());
			addressProForm.setValue("countryPro", preferences.getWorkAddress()
					.getCountry());
		}
	}

	public Boolean isValid() {
		return addressHouseForm.validate();
	}

	/**
	 * Adds the widget for the addresses to the parent.
	 */
	public void addToParent(Window preference) {
		preference.addItem(this);
	}

	@Override
	public String getTitle() {
		return WIDGET_TITLE;
	}

	/**
	 * @return the address elements for the house address form
	 */
	public DynamicForm getAddressHouseForm() {
		return addressHouseForm;
	}

	/**
	 * @param addressHouseForm
	 *            to set
	 */
	public void setAddressHouseForm(DynamicForm addressHouseForm) {
		this.addressHouseForm = addressHouseForm;
	}

	
}